/**
 * Author:yuiop
 * Date:2022/9/27 18:41
 */
package com.devops.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.devops.admin.po.*;
import com.devops.admin.service.*;
import com.devops.admin.util.Assert;
import com.devops.admin.vo.DOSK8sReleaseVo;
import com.devops.admin.vo.DOSNamespaceChart;
import com.devops.admin.vo.DOSReleaseVo;
import com.devops.constant.DevOpsConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DOSK8sReleaseServiceImpl implements DOSK8sReleaseService {
    @Autowired
    DOSReleaseOutService dosReleaseOutService;
    @Autowired
    DOSK8sServiceService dosK8sServiceService;
    @Autowired
    DOSK8sDeploymentService dosK8sDeploymentService;
    @Autowired
    DOSK8sPodService dosk8sPodService;
    @Autowired
    DOSK8sIngressService dosk8sIngressService;
    @Autowired
    DOSReleaseService dosReleaseService;
    @Autowired
    DOSK8sServiceMonitorService dosk8sServiceMonitorService;

    @Override
    public DOSK8sRelease getK8sReleaseInfo(Integer releaseId) {
        Assert.isTrueMvc(releaseId != null && releaseId > 0,"releaseId不能为空");
        DOSNamespaceChart namespaceChart = dosReleaseOutService
                .dealDbRelease(DOSK8sReleaseVo.builder().id(releaseId).type(DevOpsConstant.ReleaseType.HELM).build());


        return DOSK8sRelease.builder()
                .services(dosK8sServiceService.getK8sServiceInfo(namespaceChart))
                .deployments(dosK8sDeploymentService.getK8sDeploymentInfo(namespaceChart))
                .pods(dosk8sPodService.getK8sClusterPod(namespaceChart))
                .ingresses(dosk8sIngressService.getReleaseIngress(namespaceChart))
                .serviceMonitors(dosk8sServiceMonitorService.getK8sServiceMonitorInfo(namespaceChart))
                .build();
    }

    @Override
    public DOSK8sRelease getK8sResourceInfo(DOSK8sReleaseVo release) {
        Assert.isTrueMvc(release.getId() != null && release.getId() > 0,"id不能为null");
        Assert.isTrueMvc(release.getType() != null && release.getType() > 0,"type不能为null");
        Assert.isTrueMvc(!(release.getType() == 1 && StrUtil.isBlank(release.getKind())),"kind不能为空");
        DOSNamespaceChart namespaceChart = dosReleaseOutService.dealDbRelease(release);
        List<DOSK8sService> services = new ArrayList<>();
        List<DOSK8sDeployment> deployments = new ArrayList<>();
        List<DOSPod> pods = new ArrayList<>();
        List<DOSK8sIngressPo> ingresses = new ArrayList<>();
        if (namespaceChart.getKind().equalsIgnoreCase("pod")) {
            pods = dosk8sPodService.getK8sClusterPod(namespaceChart);
        } else if (namespaceChart.getKind().equalsIgnoreCase("deployment")) {
            pods = dosk8sPodService.getK8sClusterPod(namespaceChart);
            deployments = dosK8sDeploymentService.getK8sDeploymentInfo(namespaceChart);
        } else if (namespaceChart.getKind().equalsIgnoreCase("service")) {
            services = dosK8sServiceService.getK8sServiceInfo(namespaceChart);
        } else if (namespaceChart.getKind().equalsIgnoreCase("ingress")) {
            ingresses = dosk8sIngressService.getReleaseIngress(namespaceChart);
        }
        return DOSK8sRelease.builder()
                .pods(pods)
                .deployments(deployments)
                .services(services)
                .ingresses(ingresses)
                .build();
    }

    @Override
    public DOSK8sRelease getK8sProjectReleaseInfo(DOSReleaseVo dOSReleaseVo) {
        List<DOSReleasePo> releases = dosReleaseService.getReleases(dOSReleaseVo);
        List<Integer> helmReleaseIds = new ArrayList<>();
        List<DOSK8sService> services = new ArrayList<>();
        List<DOSK8sDeployment> deployments = new ArrayList<>();
        List<DOSPod> pods = new ArrayList<>();
        List<DOSK8sIngressPo> ingresses = new ArrayList<>();

        loadAllProjectReleaseInfo(helmReleaseIds,releases, services, deployments, pods, ingresses, DevOpsConstant.ReleaseType.HELM);
        loadAllProjectReleaseInfo(helmReleaseIds,releases, services, deployments, pods, ingresses, DevOpsConstant.ReleaseType.YAML);

        return DOSK8sRelease.builder()
                .services(services)
                .deployments(deployments)
                .pods(pods)
                .ingresses(ingresses)
                .build();
    }

    /**
     * 获取project下所有yaml、helm部署的release详情
     */
    private void loadAllProjectReleaseInfo(List<Integer> helmReleaseIds, List<DOSReleasePo> releases, List<DOSK8sService> services, List<DOSK8sDeployment> deployments, List<DOSPod> pods, List<DOSK8sIngressPo> ingresses, Integer yaml) {
        for (DOSReleasePo dosReleasePo : releases) {
            if(!helmReleaseIds.contains(dosReleasePo.getId())){
                DOSNamespaceChart namespaceChart = dosReleaseOutService
                        .dealDbRelease(DOSK8sReleaseVo.builder().id(dosReleasePo.getId()).type(yaml).build());
                if(namespaceChart != null){
                    helmReleaseIds.add(dosReleasePo.getId());
                    services.addAll(dosK8sServiceService.getK8sServiceInfo(namespaceChart));
                    deployments.addAll(dosK8sDeploymentService.getK8sDeploymentInfo(namespaceChart));
                    pods.addAll(dosk8sPodService.getK8sClusterPod(namespaceChart));
                    ingresses.addAll(dosk8sIngressService.getReleaseIngress(namespaceChart));
                }
            }
        }
    }
}
